using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Entities;
namespace DAL
{
   public  class tbChitietdhDAL
    {
        private SqlConnection cn = null;
       public tbChitietdhDAL()
       {
           cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
           cn.Open();
          
       }
       public void Insert(tbChitietdh ctdh)
       {
           SqlCommand cm = new SqlCommand("Chi_tiet_dh_Insert", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Madh", ctdh.Madh );
           cm.Parameters.AddWithValue("@Masp", ctdh.Masp );
           cm.Parameters.AddWithValue("@Soluong", ctdh.Soluong);
           cm.Parameters.AddWithValue("@Dongia", ctdh.Dongia );
           cm.Parameters.AddWithValue("@Thanhtien", ctdh.Thanhtien);
           cm.ExecuteNonQuery();
       }
       public void Update(tbChitietdh ctdh)
       {
           SqlCommand cm = new SqlCommand("Chi_tiet_dh_Update", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Mactdh", ctdh.Mactdh);
           cm.Parameters.AddWithValue("@Madh", ctdh.Madh);
           cm.Parameters.AddWithValue("@Masp", ctdh.Masp);
           cm.Parameters.AddWithValue("@Soluong", ctdh.Soluong);
           cm.Parameters.AddWithValue("@Dongia", ctdh.Dongia);
           cm.Parameters.AddWithValue("@Thanhtien", ctdh.Thanhtien);
           cm.ExecuteNonQuery();
       }
       public void Delete(int iD)
       {
           SqlCommand cm = new SqlCommand("Chi_tiet_dh_Delete", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Mactdh", iD );
           cm.ExecuteNonQuery();
       }
       public tbChitietdhCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
       {
           SqlCommand cm = new SqlCommand("Chi_tiet_dh_Get", cn);
          cm.CommandType = CommandType.StoredProcedure;
          cm.Parameters.AddWithValue("@Where", where);
          cm.Parameters.AddWithValue("@PageIndex", pageIndex);
          cm.Parameters.AddWithValue("@PageSize", pageSize);
          cm.Parameters.AddWithValue("@OrderBy", orderBy);
          cm.Parameters.AddWithValue("@OrderDirection",orderDirection);
          cm.Parameters.Add(new SqlParameter("@TotalRecords",DbType.Int32)).Direction =ParameterDirection.Output;
          SqlDataReader dr = cm.ExecuteReader();
         tbChitietdhCollection  ctdhCollection = new tbChitietdhCollection ();
         while (dr.Read())
         {
             tbChitietdh ctdh= new tbChitietdh ();
          ctdh .Mactdh  = int.Parse(dr.GetValue(0).ToString());
             ctdh .Madh  = int.Parse (dr.GetValue(1).ToString());
             ctdh.Masp  = int.Parse(dr.GetValue(2).ToString());
             ctdh.Soluong  = int.Parse(dr.GetValue(3).ToString());
             ctdh.Dongia  =float.Parse(dr.GetValue(4).ToString());
             ctdh.Thanhtien  =float.Parse(dr.GetValue(1).ToString());
             ctdhCollection.Add(ctdh);
         }
         dr.Close();
         totalRecords = int.Parse(
         cm.Parameters["@TotalRecords"].Value.ToString());
         return ctdhCollection ;
       }
       public int soluongngmua(tbChitietdh ctdh)
       {
           SqlCommand cm = new SqlCommand("chitietdh_count", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@masp", ctdh.Masp);
           return int.Parse(cm.ExecuteScalar().ToString());
       }
    }

}
